package com.fengmoxi.ctv.activity.config.lamp.pq;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;

import com.fengmoxi.ctv.R;
import com.fengmoxi.ctv.activity.config.lamp.ConfigLampActivity;
import com.fengmoxi.ctv.manage.HomeButtonReceiver;
import com.fengmoxi.ctv.manage.LampManage;
import com.litao.slider.NiftySlider;

public class ConfigLampSaturationActivity extends AppCompatActivity {
    private static final String TAG = ConfigLampSaturationActivity.class.getSimpleName();
    private NiftySlider mSliderU;
    private NiftySlider mSliderV;
    private LampManage.UV saturation;
    private LampManage mLampManage;
    private HomeButtonReceiver mHomeButtonReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_config_lamp_saturation);

        mSliderU = findViewById(R.id.seekBarU);
        mSliderV = findViewById(R.id.seekBarV);
        mLampManage = new LampManage();

        mSliderU.addOnValueChangeListener((slider, value, fromUser) -> {
            int data = Math.round(value);
            if (saturation.getU() != data) {
                saturation.setU(data);
                Log.d(TAG, "Saturation U Change to: " + saturation.getU());
                mLampManage.setSaturation(saturation);
                refresh();
            }
            return null;
        });

        mSliderV.addOnValueChangeListener((slider, value, fromUser) -> {
            int data = Math.round(value);
            if (saturation.getV() != data) {
                saturation.setV(data);
                Log.d(TAG, "Saturation V Change to: " + saturation.getV());
                mLampManage.setSaturation(saturation);
                refresh();
            }
            return null;
        });

        refresh();

        mHomeButtonReceiver = new HomeButtonReceiver(this, new HomeButtonReceiver.OnHomeBtnPressListener() {
            @Override
            public void onHomeBtnPress() {
                if (ConfigLampActivity.configLampActivity != null) {
                    ConfigLampActivity.configLampActivity.finish();
                }
                finish();
            }

            @Override
            public void onHomeBtnLongPress() {

            }
        });
        mHomeButtonReceiver.start();
    }

    @Override
    protected void onDestroy() {
        // 保存设置
        mLampManage.savePQUserParam();
        mHomeButtonReceiver.stop();
        super.onDestroy();
    }

    public boolean onKeyDown(int kCode, KeyEvent kEvent) {
        switch (kCode) {
            case KeyEvent.KEYCODE_DPAD_LEFT:
                minusU();
                return true;
            case KeyEvent.KEYCODE_DPAD_DOWN:
                minusV();
                return true;
            case KeyEvent.KEYCODE_DPAD_RIGHT:
                addU();
                return true;
            case KeyEvent.KEYCODE_DPAD_UP:
                addV();
                return true;
        }
        return super.onKeyDown(kCode, kEvent);
    }

    private void refresh() {
        LampManage.UV data = new LampManage().getSaturation();
        Log.d(TAG, "Saturation: " + data);
        if (data.getU() >= -15 && data.getU() <= 15 && data.getV() >= -15 && data.getV() <= 15)
            saturation = data;
        else
            saturation = new LampManage.UV(0, 0);
        mSliderU.setValue(saturation.getU(), false);
        mSliderV.setValue(saturation.getV(), false);
    }

    private void addU() {
        Log.d(TAG, "Saturation U add");
        if (saturation.getU() < 15) {
            saturation.setU(saturation.getU()+1);
            mSliderU.setValue(saturation.getU(), true);
        }
    }

    private void minusU() {
        Log.d(TAG, "Saturation U minus");
        if (saturation.getU() > -15) {
            saturation.setU(saturation.getU()-1);
            mSliderU.setValue(saturation.getU(), true);
        }
    }

    private void addV() {
        Log.d(TAG, "Saturation V add");
        if (saturation.getV() < 15) {
            saturation.setV(saturation.getV()+1);
            mSliderV.setValue(saturation.getV(), true);
        }
    }

    private void minusV() {
        Log.d(TAG, "Saturation V minus");
        if (saturation.getV() > -15) {
            saturation.setV(saturation.getV()-1);
            mSliderV.setValue(saturation.getV(), true);
        }
    }
}